/**
 * 
 */
package com.zcy.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.tools.internal.ws.processor.model.Request;
import com.zcy.model.User;
import com.zcy.pageModel.Puser;
import com.zcy.service.UserService;

/**
 * @author 周成瑜 此处不需要加controller注解，因为导入了spring-struts-pluningjar包
 */
@ParentPackage("basePackage")
// 这个名字是配置在struts2配置文件中的名字
@Namespace("/")
@Action(value="userAction",results={
		@Result(name="userIndex",location="/jsp/user.jsp")
		
})
public class UserAction extends BaseAction implements ModelDriven<Puser>{
	
	private static final Logger looger = Logger.getLogger(UserAction.class);
	
	private Puser puser=new Puser();
	
	
	@Override
	public Puser getModel() {
		return puser;
	}
	
	@Autowired
    private UserService userService;
	

	public void test1() {
		looger.info("hahhahaa呵呵");
		//通过web容器得到上下午环境变量而不是测试时候初始化得到
		/*ApplicationContext ac = WebApplicationContextUtils
				.getWebApplicationContext(ServletActionContext
						.getServletContext());
		UserService userService = (UserService) ac.getBean("userService");*/
		//userService.test();
	}
	
	public void add(){
		/*User u=new User();
		//u.setId((long) 11);
		u.setUsername("dsada");
		u.setPassword("dsadsadas");
		u.setCreattime(new Date());
		userService.save(u);*/
	}
	
	public String userIndex(){
		return "userIndex";
	}
	
	public void reg() throws IOException{
	/*User user=new User();
	user.setName(ServletActionContext.getRequest().getParameter("username"));
	user.setPassword(ServletActionContext.getRequest().getParameter("password"));
	user.setCreattime(new Date());
	user.setModifytime(new Date());
     System.out.println(user);*/
		
     Map<String,Object> map=new HashMap<String,Object>();
      try {
		userService.save(puser);
		map.put("success", true);
		map.put("msg", "注册成功！");
	} catch (Exception e) {
		e.printStackTrace();
		map.put("success", false);
		map.put("msg", "注册失败！");
	}
          super.writeJson(map);
	}

	public void save(){
		Map<String,Object> map=new HashMap<String,Object>();
	      try {
		map.put("success", true);
		map.put("msg", "登录成功！");
		userService.login(puser);
	      } catch (Exception e) {
	  		e.printStackTrace();
	  		map.put("success", false);
	  		map.put("msg", "登录失败！");
	  	}
	            super.writeJson(map);
	  	}

}
